package evolution.operation.migration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import evolution.algorithm.CoevolutionAlgorithm;
import evolution.executor.EvolutionaryAlghorithmEpochRunner;
import evolution.individual.Individual;

public class TwoSidedCircleMigration extends AbstractMigrationStrategy {

	@Override
	public void migrate(CoevolutionAlgorithm coevolutionAlghoritm) {
		List<EvolutionaryAlghorithmEpochRunner> evolutionaryAlghorithmEpochRunners = coevolutionAlghoritm
				.getEvolutionaryAlghorithmEpochRunners();
		Individual[][] populations = MigrationHelper
				.extractPopulations(evolutionaryAlghorithmEpochRunners);
		Individual[][] bestLists = MigrationHelper.buildBestIndividualsLists(
				populations, migrationSize);

		int length = bestLists.length;
		for (int i = 0; i < length; i++) {
			Individual[] best = bestLists[(i + 1) % length];
			Individual[] populationLeft = populations[i % length];
			Individual[] populationRight = populations[(i + 2) % length];

			MigrationHelper.replaceLastN(best, populationLeft, migrationSize);
			MigrationHelper.replaceLastN(best, populationRight, migrationSize,
					migrationSize);
		}
		
		List<Individual> allPopulations = new ArrayList<>();
		for (EvolutionaryAlghorithmEpochRunner runner : evolutionaryAlghorithmEpochRunners) {
			Individual[] population = runner.getEvolutionaryAlghorithm()
					.getPopulation();

			allPopulations.addAll(Arrays.asList(population));
		}

		// for (int i = 0; i < allPopulations.size(); i++) {
		// for (int j = 0; j < allPopulations.size(); j++) {
		// if (i != j) {
		// if (allPopulations.get(i) == allPopulations.get(j)) {
		// System.out.println(".");
		// }
		// }
		// }
		// }
	}
	
	

}
